/**
 * 
 */
package com.swg.cocobox.message.example;

import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.log4j.Logger;

import com.swg.cocobox.message.BroadcastMessage;
import com.swg.cocobox.message.ClientInfo;
import com.swg.cocobox.message.ClientValidator;
import com.swg.cocobox.message.Message;
import com.swg.cocobox.message.MessageContext;
import com.swg.cocobox.message.MessageServer;
import com.swg.cocobox.message.PrivateMessage;
import com.swg.cocobox.message.event.MessageEvent;
import com.swg.cocobox.message.event.MessageEventListener;

/**
 * @author satriaprayoga
 *
 */
public class SimpleMessageEventListener implements MessageEventListener {
	
	private static Logger logger=Logger.getLogger(SimpleMessageEventListener.class);
	
	private ClientValidator validator;
	
	public SimpleMessageEventListener() {
		validator=new MockClientValidator();
	}

	@Override
	public void onConnection(MessageEvent event) {
		logger.info("accepting connection from: "+event.getChannel().socket().getRemoteSocketAddress());
	}

	@Override
	public void onReceiving(MessageEvent event) {
		if(event.getAttachment()!=null){
			handleLogin(event);
			//handlePrivateMessage(event);
			handleMessage(event);
		}
	}

	@Override
	public void onWriting(MessageEvent event) {
		// TODO Auto-generated method stub
		
	}
	
	private void handleLogin(MessageEvent event){
		if(ClientInfo.class.isAssignableFrom(event.getAttachment().getClass())){
			try{
				ClientInfo clientInfo=(ClientInfo)event.getAttachment();
				MessageServer server=(MessageServer)event.getSource();
				if(validator.isValid(clientInfo)){
					clientInfo.setChannel(event.getChannel());
					server.getClientInfoList().addClientInfo(clientInfo);
					logger.info("login request: "+clientInfo.getUsername()+" logged user: "+server.getClientInfoList().count());
					server.send(event.getChannel(), new String(""+MessageContext.LOGIN_OK).getBytes());
				}else{
					logger.info("login request: "+clientInfo.getUsername()+" loggin error"); 
					byte[] logginError="login error\n\r".getBytes(Charset.forName("US-ASCII"));
					server.send(event.getChannel(), logginError);
				}
			}catch(IOException e){
				
			}
			
			
		}
	}
	
	private void handleMessage(MessageEvent event){
		if(Message.class.isAssignableFrom(event.getAttachment().getClass())){
			try{
				Message message=(Message)event.getAttachment();
				MessageServer server=(MessageServer)event.getSource();
				if(message.getMode()==MessageContext.BROADCAST){
					BroadcastMessage broadcastMessage=(BroadcastMessage)message;
					logger.info("broadcast message from: "+broadcastMessage.getOrigin());
					for(ClientInfo info:server.getClientInfoList()){
						logger.info("include to broadcast: "+info.getUsername());
						server.sendBroadcast(info.getChannel(), broadcastMessage.getBytes());
					}
				}else if(message.getMode()==MessageContext.PRIVATE){
					PrivateMessage privateMessage=(PrivateMessage)message;
					ClientInfo info=server.getClientInfoList().getByUsername(privateMessage.getDestination());
					logger.info("private message from: "+privateMessage.getOrigin()+", destination: "+info.getUsername());
					server.sendPrivate(info, privateMessage.getBytes());
					
				}
			}catch(IOException e){
				
			}
		}
	}

}
